package lovemusic.lanou.com.newsmusic.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

import lovemusic.lanou.com.newsmusic.R;
import lovemusic.lanou.com.newsmusic.utils.UIUtils;

/**
 * 判断加载的是哪一个页面
 * Created by lanouhn on 2016/9/28.
 */
public abstract class LoadingPage extends FrameLayout{

    private static final int STATE_LOAD_UNDO = 1;// 未加载
    private static final int STATE_LOAD_LOADING = 2;// 正在加载
    private static final int STATE_LOAD_ERROR = 3;// 加载失败
    private static final int STATE_LOAD_EMPTY = 4;// 数据为空
    private static final int STATE_LOAD_SUCCESS = 5;// 加载成功

    private View mLoadingPage;
    private View mErrorPage;
    private View mEmptyPage;
    private View mSuccessPage;

    private int mCurrentState = STATE_LOAD_UNDO;// 当前状态

    public LoadingPage(Context context) {
        super(context);
        initView();
    }



    public LoadingPage(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public LoadingPage(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
//        初始化加载布局
        if (mLoadingPage==null){
            mLoadingPage= UIUtils.inflate(R.layout.page_loading);
            addView(mLoadingPage);
        }
//      初始化加载失败的布局
        if (mErrorPage==null){
            mErrorPage=UIUtils.inflate(R.layout.page_error);
//       点击按钮刷新重试
            Button btnRetry= (Button) mErrorPage.findViewById(R.id.btn_retry);
            btnRetry.setOnClickListener(new OnClickListener() {


                @Override
                public void onClick(View v) {
                    // 重新加载网络数据

                }
            });
            addView(mErrorPage);
        }

//       初始化数据为空
        if (mEmptyPage==null){
            mEmptyPage=UIUtils.inflate(R.layout.page_empty);
           addView(mEmptyPage);
        }

        showRightPage();
    }

//      根据当前状态，决定要显示那个布局
    private void showRightPage() {
        mLoadingPage
                .setVisibility((mCurrentState == STATE_LOAD_UNDO || mCurrentState == STATE_LOAD_LOADING) ? View.VISIBLE
                        : View.GONE);

        mErrorPage
                .setVisibility(mCurrentState == STATE_LOAD_ERROR ? View.VISIBLE
                        : View.GONE);

        mEmptyPage
                .setVisibility(mCurrentState == STATE_LOAD_EMPTY ? View.VISIBLE
                        : View.GONE);

        // 当成功布局为空,并且当前状态为成功,才初始化成功的布局
        if (mSuccessPage==null&&mCurrentState==STATE_LOAD_SUCCESS){
            mSuccessPage=onCreteSuccessView();
            if (mSuccessPage!=null){
                addView(mSuccessPage);
            }
        }
        if (mSuccessPage!=null){
            mSuccessPage.setVisibility(mCurrentState==STATE_LOAD_SUCCESS?VISIBLE:GONE);


            			new Thread() {
				@Override
				public void run() {
					final ResultState resultState = onLoad();
					// 运行在主线程
					UIUtils.runOnUIThread(new Runnable() {
						@Override
						public void run() {
							if (resultState != null) {
								mCurrentState = resultState.getState();// 网络加载结束后,更新网络状态
								// 根据最新的状态来刷新页面
								showRightPage();
							}
						}
					});
				}
			}.start();
        }

    }

//    开始加载数据
    public void loadDate(){
        if (mCurrentState!=STATE_LOAD_LOADING){
            mCurrentState=STATE_LOAD_LOADING;
        }
    }
    // 加载成功后显示的布局, 必须由调用者来实现
    public abstract View onCreteSuccessView();

    // 加载网络数据, 返回值表示请求网络结束后的状态
    public abstract ResultState onLoad();

    public enum ResultState {
        STATE_SUCCESS(STATE_LOAD_SUCCESS), STATE_EMPTY(STATE_LOAD_EMPTY), STATE_ERROR(
                STATE_LOAD_ERROR);

        private int state;

        private ResultState(int state) {
            this.state = state;
        }

        public int getState() {
            return state;
        }
    }






}
